<%--                                                                       --%>
<%-- Copyright (c) 2004-2005 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.broadvision.emarketing.action.Constants" %>
<%@ page import="com.broadvision.emarketing.criteriabuilder.Tree" %>

<c:set var="formBean" value="${emSegmentEditForm}" />
<c:set var="formBeanMap" value="${formBean.map}" />

<c:set var="viewBean" value="${emSegmentEditView}" />
<c:set var="viewBeanMap" value="${viewBean.map}" />
<c:set var="isNewSegment" value="${formBeanMap.segmentOid == 0}" />

<script language="JavaScript">

function doCancel(theForm) {
  <c:choose>
    <c:when test="${isNewSegment}">
      var newAction = "<html:rewrite page="/em/segment/segmentBrowse.do" />";
    </c:when>
    <c:otherwise>
      var newAction = "<html:rewrite page="/em/segment/segmentView.do" />";
    </c:otherwise>
  </c:choose>
  theForm.action = newAction;
  theForm.submit();
}

function doReset(theForm) {
  <c:choose>
    <c:when test="${isNewSegment}">
      <c:url var="newAction" value="/em/segment/segmentNew.do">
        <c:param name="currentCategoryOid" value="${formBeanMap.currentCategoryOid}"/>
      </c:url>
    </c:when>
    <c:otherwise>
      <c:url var="newAction" value="/em/segment/segmentEdit.do">
        <c:param name="segmentOid" value="${formBeanMap.segmentOid}"/>
        <c:param name="currentCategoryOid" value="${formBeanMap.currentCategoryOid}"/>
      </c:url>
    </c:otherwise>
  </c:choose>
  location = '<c:out value="${newAction}" />';
  return false;
}

function callBackForPopup(catId, catFullPath) {
  // Add code according to your need
  var theForm = document.emSegmentEditForm;
  theForm.parentCategoryOid.value = catId;
  theForm.categoryFullPath.value = catFullPath;
}

function isRootCategoryClicked(theForm, isRootSelected) {
  if (isRootSelected) {
    theForm.selectCategoryBtn.disabled = true;
    theForm.parentCategoryOid.value = 0;
    theForm.categoryFullPath.value="/";
  } else {
    theForm.selectCategoryBtn.disabled = false;
  }
  return true;
}

function doEditCriteria(theForm) {
  theForm.action = '<html:rewrite page="/em/segment/segmentModCriteria.do" />';
  theForm.submit();
  return false;
}

function doSave(theForm) {
  theForm.name.value = theForm.name.value.replace(/^\s+/g, '').replace(/\s+$/g, '');
  return submitForm(theForm);
}
</script>

<jsp:include page="/mt/common/pickerPopupHelper.jsp" flush="true" />

<html:javascript formName="emSegmentEditForm" method="submitForm" />

<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <c:choose>
          <c:when test="${isNewSegment}">
            <bean-el:message key="em.segment.edit.page.title.create" bundle="em"/>
          </c:when>
          <c:otherwise>
            <bean-el:message key="em.segment.edit.page.title.edit" bundle="em" arg0="${viewBeanMap.segmentPersistentName}"/>
          </c:otherwise>
        </c:choose>
      </div>
      <div class="errorMessage"><html:errors bundle="em"/></div>
      <!- place body here -->

      <html:form action="/em/segment/segmentSave.do" focus="name">

      <html:hidden property="segmentOid"/>
      <html:hidden property="currentCategoryOid"/>
      <html:hidden property="displayMode"/>
      <html:hidden property="parentCategoryOid"/>
      <input type="hidden" name="fromType" value="segment">
      <c:choose>
        <c:when test="${isNewSegment}">
          <input type="hidden" name="actionType" value="create">
        </c:when>
        <c:otherwise>
          <input type="hidden" name="actionType" value="edit">
        </c:otherwise>
      </c:choose>

      <div class="pageSubtitle">
        <span class="left">
          <c:choose>
            <c:when test="${isNewSegment}">
              <bean-el:message key="em.segment.edit.page.subtitle.create" bundle="em"/>
            </c:when>
            <c:otherwise>
              <bean-el:message key="em.segment.edit.page.subtitle.edit" bundle="em"/>
            </c:otherwise>
          </c:choose>
        </span>
        <span class="right">&nbsp;</span>
      </div>

      <table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td class="label" width="20%"><bean-el:message key="em.segment.name" bundle="em"/>:*</td>
        <td class="labelField" width="80%">
            <html:messages id="nameError" property="name" bundle="em" />
            <c:if test="${not empty nameError}">
              <span class="errorMessage">
                <c:out value="${nameError}" />
              </span>
              <br>
            </c:if>
          <html:text property="name" size="50"/>
        </td>
      </tr>

      <tr>
        <td class="label"><bean-el:message bundle="mc.messages" key="mc.header.description"/>:</td>
        <td class="labelField"><html:textarea property="description" cols="50" rows="5"/></td>
      </tr>

      <tr>
        <td class="label"><bean-el:message key="em.label.status" bundle="em"/>:</td>
        <td class="labelField">
          <html:radio property="status" value="1"><bean-el:message bundle="em" key="em.label.online" /></html:radio>
          <html:radio property="status" value="0"><bean-el:message bundle="em" key="em.label.offline" /></html:radio>
        </td>
      </tr>

      <tr>
        <td class="label"><bean-el:message key="em.segment.category" bundle="em"/>:</td>
        <td class="labelField">
          <table border="0">
            <tr>
              <td>
                <html:textarea name="viewBean" property="categoryFullPath" cols="50" rows="2" readonly="true" />
              </td>
              <td align="left">
                <c:set var="rootCheckedStr">
                  <c:if test="${formBeanMap.parentCategoryOid == 0}">checked</c:if>
                </c:set>
                <input type="radio" <c:out value='${rootCheckedStr}' /> name="isRootCategory" value="true" onclick="return isRootCategoryClicked(this.form, true)">
                <bean-el:message bundle="em" key="em.segment.edit.radiobutton.uncategorize" />
                <br>
                <c:set var="nonRootCheckedStr">
                  <c:if test="${formBeanMap.parentCategoryOid != 0}">checked</c:if>
                </c:set>
                <input type="radio" <c:out value='${nonRootCheckedStr}' /> name="isRootCategory" value="false" onclick="return isRootCategoryClicked(this.form, false)">
                <c:set var="disableSelectCategoryBtn">
                  <c:if test="${formBeanMap.parentCategoryOid == 0}">disabled=true</c:if>
                </c:set>
                <input type="button" name="selectCategoryBtn"
                  <c:out value="${disableSelectCategoryBtn}" />
                  value="<bean-el:message bundle="em" key="em.segment.button.select.category" />"
                  class="inputButton" onclick="return doPickerPopupAction('<html:rewrite page="/mt/categoryPickerPopup.do"/>', 'callBackForPopup', <c:out value="${viewBeanMap.segmentCntTypeId}" />, this.form.parentCategoryOid.value, null, true, false, false);" >
              </td>
            </tr>
          </table>
        </td>
      </tr>
      <c:if test="${not isNewSegment}" >
        <%-- This is an old segment. --%>
        <tr>
          <td class="label"><bean-el:message bundle="mc.messages" key="mc.header.lastModifiedDate"/>:</td>
          <td class="labelField"><bean:write name="viewBeanMap" property="lastModifiedTime" bundle="em" formatKey="format.datetime.long"/></td>
        </tr>

        <tr>
          <td class="label"><bean-el:message bundle="em" key="em.segment.creation.date" />:</td>
          <td class="labelField"><bean:write name="viewBeanMap" property="creationTime" bundle="em" formatKey="format.datetime.long"/></td>
        </tr>
      </c:if>
      </table>


      <div class="pageSubtitle">
        <span class="left"><bean-el:message key="em.segment.view.page.subtitle.2" bundle="em"/></span>
        <span class="right">
          <input type="button" name="modCriteriaBtn" class="inputButton" value="<bean-el:message key='em.segment.button.modifyCriteria' bundle="em"/>" onclick="javascript:return doEditCriteria(this.form)">
        </span>
      </div>
      <%
         Tree conditionTree = (Tree)session.getAttribute(Constants.CRITERIA_TREE);
         request.setAttribute("conditionList", conditionTree.getPreorderNodeList(false, false));
      %>
      <jsp:include page="/em/criteriabuilder/conditionListViewSection.jsp" />

      <div class="pageButtonWrap" align="right">
        <input type="button" name="resetBtn" class="inputButton" value="<bean-el:message key='button.reset' bundle="em"/>" onclick="javascript:return doReset(this.form)">
        &nbsp;&nbsp;
        <input type="button" name="cancelBtn" class="inputButton" value="<bean-el:message key='button.cancel' bundle="em"/>" onclick="javascript:return doCancel(this.form)">

        <c:set var="lastButtonValue">
          <c:choose>
            <c:when test="${isNewSegment}" >
              <bean-el:message key='button.save' bundle="em"/>
            </c:when>
            <c:otherwise>
              <bean-el:message key='button.update' bundle="em"/>
            </c:otherwise>
          </c:choose>
        </c:set>
        <input type="submit" name="saveBtn" class="inputButton"
            value='<c:out value="${lastButtonValue}" />'
            onclick="JavaScript:return doSave(this.form)">
      </div>

      </html:form>

  </td></tr>
</table>